package com.m7788.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.webkit.WebView;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.google.gson.Gson;
import com.m7788.screen.R;
import com.mob.MobSDK;
import com.tool.utils.DataUtil;

public class ShareUtil {
	private String type;
	private Context context;
	private WebView webview;
	private int platformType;
	public ShareUtil(Context context) {
		this.context = context;
	}
	
	public ShareUtil(Context context, WebView webview) {
		this.context = context;
		this.webview = webview;
		MobSDK.init(context);
	}

	/**
	 * 
	 * 方法名：showShare 功能： 参数：
	 * 
	 * @param title
	 * @param content
	 * @param webUrl
	 *            网页地址
	 * @param imageUrl
	 *            链接图片地址 创建人：huanghaishui 创建时间：2015-11-3
	 */
	public void showShare(String title, String content, String webUrl,
			String imageUrl, String type) {
		// Log.e("webUrl", webUrl);
		String platform = null;

		if ("1".equals(type)) {
			platform = Wechat.NAME;
		} else if ("2".equals(type)) {
			platform = WechatMoments.NAME;
		} else if ("3".equals(type)) {
			platform = SinaWeibo.NAME;
		} else if ("4".equals(type)) {
			platform = QQ.NAME;
		} else if ("5".equals(type)) {
			platform = QZone.NAME;
		}
		final OnekeyShare oks = new OnekeyShare(); //
		oks.setAddress("");
		oks.setTitle(title);
		oks.setTitleUrl(webUrl);
		oks.setText(content);
		oks.setImageUrl(imageUrl);
		oks.setUrl(webUrl);
		oks.setComment(content);
		oks.setSite(getStringValue(R.string.app_name));
		oks.setSiteUrl(webUrl);
		oks.setVenueName(getStringValue(R.string.app_name));
		oks.setVenueDescription("This is a beautiful place!");
		// oks.setLatitude(23.056081f);
		// oks.setLongitude(113.385708f);
		oks.setSilent(false);
		if (platform != null) {
			oks.setPlatform(platform);
		}
		// 去除注释，可令编辑页面显示为Dialog模式
		oks.setDialogMode(true);

		oks.show(context);
	}

	Platform mPlatform = null;

	public void login(int platform) {
		this.platformType = platform;
		type = "login";
		String name = null;
		if (0 == platform) {
			name = QQ.NAME;
			if (!DataUtil.isHaveApp("com.tencent.mobileqq", context)) {
				JSONObject jsonObject = new JSONObject();
				String callback = "app_check_callback";
				webview.loadUrl("javascript:" + callback
						+ "("+platform+")");
			}
			else{
				mPlatform = ShareSDK.getPlatform(name);
				mPlatform.setPlatformActionListener(mPlatformActionListener);
				//mPlatform.authorize();// 单独授权,OnComplete返回的hashmap是空的
				mPlatform.showUser(null);// 授权并获取用户信息
			}
		} else if (1 == platform) {
			name = SinaWeibo.NAME;
			if (!DataUtil.isHaveApp("com.sina.weibo", context)) {
				JSONObject jsonObject = new JSONObject();
				String callback = "app_check_callback";
				webview.loadUrl("javascript:" + callback
						+ "("+platform+")");
			}
			else{
				mPlatform = ShareSDK.getPlatform(name);
				mPlatform.setPlatformActionListener(mPlatformActionListener);
				//mPlatform.authorize();// 单独授权,OnComplete返回的hashmap是空的
				mPlatform.showUser(null);// 授权并获取用户信息
			}
		} else if (2 == platform) {
			name = Wechat.NAME;
			mPlatform = ShareSDK.getPlatform(name);
			mPlatform.setPlatformActionListener(mPlatformActionListener);
			//mPlatform.authorize();// 单独授权,OnComplete返回的hashmap是空的
			mPlatform.showUser(null);// 授权并获取用户信息
		}
	}

	public PlatformActionListener mPlatformActionListener = new PlatformActionListener() {
		@Override
		public void onComplete(Platform platform, int i,
				HashMap<String, Object> hashMap) {
			if (type.equals("login")) {
				Log.e("onComplete", "登录成功");
				Log.e("openid", platform.getDb().getUserId());// 拿到登录后的openid
				Log.e("username", platform.getDb().getUserName());// 拿到登录用户的昵称
				// DataUtil.showShortToast(context,
				// platform.getDb().getUserId()+","+platform.getDb().getUserName());
			} else {
				Log.e("onComplete", "分享成功");
				// DataUtil.showLongToast(context, "分享成功");
			}
			final Map<String, Object> result = null == hashMap ? new HashMap<String, Object>()
					: hashMap;
			((Activity) context).runOnUiThread(new Runnable() {

				@Override
				public void run() {
					String method = "app_outside_login_callback";
					webview.loadUrl("javascript:" + method + "('" + new Gson().toJson(result) + "', "+platformType+")");					
					mPlatform.removeAccount(true);
					DataUtil.showShortToast(context, "登录成功");
				}
			});

		}

		@Override
		public void onError(Platform platform, int i, Throwable throwable) {
			Log.e("onError", throwable.toString() + "");
			if (type.equals("login")) {
				Log.e("onError", "登录失败" + throwable.toString());
			} else {
				Log.e("onError", "分享失败" + throwable.toString());
			}
			((Activity) context).runOnUiThread(new Runnable() {

				@Override
				public void run() {
					 DataUtil.showShortToast(context, "登录失败");
				}
			});
		}

		@Override
		public void onCancel(Platform platform, int i) {
			if (type.equals("login")) {
				Log.e("onCancel", "登录取消");
			} else {
				Log.e("onCancel", "分享取消");
			}
			((Activity) context).runOnUiThread(new Runnable() {

				@Override
				public void run() {
					 DataUtil.showShortToast(context, "取消登录");
				}
			});
		}
	};

	private String getStringValue(int sid) {
		return context.getResources().getString(sid);
	}
}
